import numpy as np
from tqdm import trange
np.set_printoptions(suppress=True)

# infilepath=r"meta_data_batch\meta_data_batch4.txt"
def packing_attack_data():
    infilepath=r"seed_attack.txt"
    with open(infilepath, "r", encoding='utf-8') as f:
        for data in f.readlines():
            if data.find("pk=")!=-1:
                x=data[3:].strip()
    pk=[]
    pk.append(x)
    with open(infilepath, "r", encoding='utf-8') as f:
        count=0
        part=0
        group=0
        maxsize=4000
        blocksize=500

        for data in f.readlines():
            if count>maxsize:
                break   
            if count%blocksize==0 and group==0:
                if count!=0 and len(sm)==blocksize:
                    print(count)
                    savepath=r"Test_Metadata\metadata_part{}".format(part)
                    pk_str=np.array(pk,dtype=object)
                    msg_array=np.array(msg,dtype=object)
                    sm_array=np.array(sm,dtype=object)
                    np.savez_compressed(savepath,pk=pk_str,counter=counter,msg=msg_array,mlen=mlen,sm=sm_array,smlen=smlen,rejection=nonce,index=index,allbytes=allbytes,zero=zero)
                    part+=1
                
            
                counter=np.zeros([blocksize,],dtype=int)
                mlen=np.zeros([blocksize,],dtype=int)
                sm=[]
                msg=[]
                allbytes=[]
                smlen=np.zeros([blocksize,],dtype=int)
                nonce=np.zeros([blocksize,],dtype=int)
                index=np.zeros([blocksize,],dtype=int)
                zero=np.zeros([blocksize,],dtype=int)
        
        
                

            if data.find("counter=")!=-1:
                counter[count%blocksize]=(int(data[8:]))
                group+=1

            if data.find("mark=")!=-1:
                # print(data[7])
                if data[5:]=="1 1 1 1 \n":
                    zero[count%blocksize]=-1
                elif data[5:]=="0 1 1 1 \n":
                    zero[count%blocksize]=0
                elif data[5:]=="1 0 1 1 \n":
                    zero[count%blocksize]=1
                elif data[5:]=="1 1 0 1 \n":
                    zero[count%blocksize]=2
                elif data[5:]=="1 1 1 0 \n":
                    zero[count%blocksize]=3
                group+=1

            if data.find("nonce=")==0:
                nonce[count%blocksize]=int(data[6:],16)-1
                group+=1

            if data.find("mark_index=")==0:
                index[count%blocksize]=int(data[11:])
                group+=1

            if data.find("msg=")!=-1:
                msg.append(data[4:].strip())
                group+=1
                
            if data.find("r_allbytes")!=-1:
                allbytes.append(data[14:].strip())
                group+=1

            if data.find("mlen=")==0:
                mlen[count%blocksize]=(int(data[5:]))
                group+=1
        
            if data.find("sm=")==0 and data.find("smlen=")==-1:
                sm.append(data[3:].strip())
                group+=1

            if data.find("smlen=")!=-1:
                smlen[count%blocksize]=int(data[6:])
                group+=1
            
            if group==9:
                group=0
                # print(count)
                count+=1


if __name__ == '__main__':
    packing_attack_data()
    # f1=np.load(r"D:\Dilithium_Paper_Work\Dilithium_paper_metadata\metadata_files\attacking_metadata\metadata_attacking_part3000.npz",allow_pickle=True)
    # print(f1.files)
    # print(f1["rejection"])
    
